/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servidor.Controlador;


import BaseDatos.FachadaBBDD;
import Servidor.Modelo.Users.User;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

/**
  * This class creates a Thread for get a user by his identifier
 * @author isabel
 * @author Jorge Perez
 */
public class QueryByCodeUserTask extends Thread implements OcaTask{

    private Socket incoming;
    private BufferedReader read;
    private PrintWriter write;
    
    public QueryByCodeUserTask(Socket incoming, BufferedReader read, PrintWriter write) {
        this.incoming = incoming;
        this.read = read;
        this.write = write;
    }
    

    @Override
    public void executeTask() {
        this.start();
    }

    @Override
    public void run() {
        int idUser = 0;
        
        try {
            String idUserString = read.readLine();
            idUser = Integer.parseInt(idUserString);
        } catch (IOException ex) {
            System.out.println("No se pudo leer correctamente - QueryUserTask");
        } catch (NumberFormatException ex) {
            System.out.println("El idUser no es un numero entero - QueryUserTask");
        }
        
        User user = FachadaBBDD.get_instance().select_user(idUser);
        String userString;
        userString = String.valueOf(user.getId_user()) + "#";
        userString += String.valueOf(user.getNick()) + "#";
        userString += String.valueOf(user.getEmail()) + "#";
        userString += String.valueOf(user.getType_user());

        write.println(userString);
        
        new TrashTask(incoming, read, write);
    }
    
    
}
